package com.restphp.java.utils.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DomainValidateUtils.
 *
 * @author zj.
 *         Created on 2018/11/26 0026.
 */
public class DomainValidateUtils {
    /**
     * 是否为域名.
     * @param val to check value.
     * @return result.
     */
    public static Boolean isDomain(String val) {
        return isEnDomain(val) || isCnDomain(val);
    }
    /**
     * 是否英文域名.
     * @param val to check value.
     * @return result.
     */
    public static Boolean isEnDomain(String val) {
        Pattern pattern = Pattern.compile("^[a-z0-9\\-]+\\.([a-z]{2,6}\\.){0,1}[a-z]{2,6}$");
        Matcher m = pattern.matcher(val);
        return m.find();
    }

    /**
     * 是否为中文域名.
     * @param val to check value.
     * @return result.
     */
    public static Boolean isCnDomain(String val) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9_\\u4E00-\\u9FA5]+([\\.\\-][A-Za-z0-9_\\u4E00-\\u9FA5]+)*$");
        Matcher m = pattern.matcher(val);
        return m.find();
    }

    public static void main(String[] args) {
        System.out.println(isCnDomain("中国.com"));
    }
}
